package com.webserver.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 响应对象
 * 该类的每一个实例用于表示HTTP协议要求的响应内容
 * 一个响应由三部分构成
 * 状态行,响应头,响应正文
 */
public class HttpResponse {
    //状态行相关信息
    private int statusCode = 200;//默认值200
    private String statusReason = "OK";

    //响应头相关信息
    private Map<String,String> headers = new HashMap<>();

    //响应正文相关信息
    private File entity;//正文对应的实体文件

    private Socket socket;

    public HttpResponse(Socket socket) {
        this.socket = socket;
    }

    /**
     * 将当前响应对象内容以标准的响应格式发送给客户端
     */
    public void flush() throws IOException {
        //1.发送状态行
        sendStatusLine();
        //2.发送响应头
        sendHeaders();
        //3.发送响应正文
        sendContent();
    }

    private void sendStatusLine() throws IOException {
        String line = "HTTP/1.1"+" "+statusCode+" "+statusReason;
        println(line);
    }

    private void sendHeaders() throws IOException {
//        String line = "Content-Type: text/html";
//        println(line);
//        line = "Content-Length: " + entity.length();
//        println(line);

        //遍历headers将所有响应头发送
        Set<Map.Entry<String,String>> entrySet = headers.entrySet();
        for(Map.Entry<String,String> e : entrySet){
            String name = e.getKey();//响应头名字
            String value = e.getValue();//响应头值
            String line = name+": "+value;
            println(line);
        }

        //单独发送CRLF表示消息头部分发送完毕
        println("");
    }

    private void sendContent() throws IOException {
        try(
            FileInputStream fis = new FileInputStream(entity)
        ) {
            OutputStream out = socket.getOutputStream();
            int len;
            byte[] buf = new byte[1024 * 10];
            while ((len = fis.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
        }
    }

    private void println(String line) throws IOException {
        OutputStream out = socket.getOutputStream();
        byte[] data = line.getBytes("ISO8859-1");
        out.write(data);
        out.write(13);
        out.write(10);
    }

    public File getEntity() {
        return entity;
    }

    /**
     *  向响应中添加响应正文的实体文件,该方法会同时根据文件添加对应的响应头
     *  Content-Type和Content-Length
     * @param entity
     */
    public void setEntity(File entity) {
        this.entity = entity;
        String ext = entity.getName().substring(entity.getName().lastIndexOf(",")+1);
        String Type = HttpContext.getMimeType(ext);
        addHeader("Content-Type",Type);
        addHeader("Content-Length",entity.length()+"");
    }

    public int getStatusCode() {
        return statusCode;
    }

    public void setStatusCode(int statusCode) {
        this.statusCode = statusCode;
    }

    public String getStatusReason() {
        return statusReason;
    }

    public void setStatusReason(String statusReason) {
        this.statusReason = statusReason;
    }

    /**
     * 添加一个要发送的响应头
     * @param name
     * @param value
     */
    public void addHeader(String name,String value){
        headers.put(name, value);
    }
}